package com.foreverwin.mes.so.serviceext;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mes.core.utils.SpringUtil;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.meapi.model.Bom;
import com.foreverwin.mes.meapi.model.BomComponent;
import com.foreverwin.mes.meapi.model.ShopOrder;
import com.foreverwin.mes.meapi.service.BomService;
import com.foreverwin.mes.meapi.service.ShopOrderService;
import com.sap.me.activity.ActivityRuntimeType;
import com.sap.me.activity.ExecutionType;
import com.sap.me.activity.ServiceExtension;
import com.sap.me.common.ObjectReference;
import com.sap.me.demand.ShopOrderConfiguration;
import com.sap.me.demand.ShopOrderFullConfiguration;
import com.sap.me.demand.ShopOrderServiceInterface;
import com.sap.me.demand.ShopOrderStatus;
import com.sap.me.extension.Services;
import com.sap.me.frame.service.CommonMethods;
import com.sap.me.productdefinition.ItemBasicConfiguration;
import com.sap.me.productdefinition.ItemConfigurationServiceInterface;
import com.visiprise.globalization.util.DateTimeInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;

/**
 * MES工单创建服务扩展
 *
 * @author Syngna
 * @date 2020/2/25 21:18
 */
@ExecutionType(ActivityRuntimeType.SYNC)
public class DeleteShopOrderExtension extends ServiceExtension<Object> {
    private final Logger logger = LoggerFactory.getLogger(DeleteShopOrderExtension.class);

    private ShopOrderServiceInterface shopOrderServiceInterface;

    @Override
    public void execute(Object request) throws Exception {
        if (request instanceof ObjectReference) {
            execute((ObjectReference) request);
        }
    }

    public void execute(ObjectReference objectReference) throws Exception {
        logger.info("delete shop order service extension start");
        String site = CommonMethods.getSite();
        shopOrderServiceInterface = Services.getService("com.sap.me.demand", "ShopOrderService", site);
        ShopOrderConfiguration shopOrderConfiguration = shopOrderServiceInterface.findShopOrderConfiguration(objectReference);
        if (ShopOrderStatus.Hold.equals(shopOrderConfiguration.getStatus())) {
            logger.info("delete shop order service extension skipped with hold status");
            return;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("PlannedOrder", StringUtils.trimHandle(objectReference.getRef()));
        jsonObject.put("ProductionPlant", site);
        jsonObject.put("QuantityIsFixed", "Delete"); // Create创建，Update修改，Delete删除
        logger.info("send shop order to erp : " + jsonObject.toJSONString());
    }
}
